<template>
  <div>
    <el-dropdown>
      <SvgIcon
        name="translate"
        color="#a52a2a"
      />
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item
            v-for="item in languages"
            :key="item.value"
            :disabled="language === item.value"
          >
            <span @click="handleSetLanguage(item.value)">{{ item.name }}</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script lang="ts" setup>
import { commonStore } from '@/store/commonStore'
import { computed, reactive } from 'vue'
import { useI18n } from 'vue-i18n'
import { ElMessage } from 'element-plus'
import SvgIcon from '../SvgIcon/index.vue'
type Language = {
  name: string
  value: string
}

const { t } = useI18n()
const store = commonStore()
const { locale } = useI18n()

const languages = reactive(
  [
    { name: 'en', value: 'en' },
    { name: '中文', value: 'zh-cn' }
  ] as Array<Language>
)

const handleSetLanguage = (lang: string) => {
  locale.value = lang
  store.setLanguage(lang)
  ElMessage({
    message: t('common.switchLanguageSuccess'),
    type: 'success'
  })
}

const language = computed(() => {
  return store.language
})

</script>

<style lang="scss" scoped>
</style>
